package com.zhangyj.servlets;

import javax.servlet.http.HttpServlet;

public class ServletImpl extends HttpServlet {
	private static final long serialVersionUID = 2697785560767579557L;
	//
	// private Map<String, String> serviceMap = new HashMap<String, String>();
	//
	// @SuppressWarnings("unchecked")
	// public void init() throws ServletException {
	// super.init();
	// try {
	// String path =
	// ServletImpl.class.getResource("/serviceConf.xml").getPath();
	// InputStream in = new FileInputStream(path);
	// Document dom = new SAXReader().read(in);
	// Iterator services =
	// dom.getRootElement().element("services").elementIterator();
	// while (services.hasNext()) {
	// Element service = (Element) services.next();
	// serviceMap.put(service.attributeValue("name"), service.getText());
	// }
	// in.close();
	// } catch (IOException ioe) {
	// ioe.printStackTrace();
	// } catch (DocumentException e) {
	// e.printStackTrace();
	// }
	//
	// }
	//
	// protected void doGet(HttpServletRequest request, HttpServletResponse
	// response) throws ServletException, IOException {
	// doPost(request, response);
	// }
	//
	// @SuppressWarnings("unchecked")
	// protected void doPost(HttpServletRequest request, HttpServletResponse
	// response) throws ServletException,
	// IOException {
	// Enumeration<String> enumeration = request.getHeaderNames();
	// while(enumeration.hasMoreElements()) {
	// String name = enumeration.nextElement();
	// System.out.println(name +"--"+ request.getHeader(name) );
	// }
	// StringBuffer buffer = new StringBuffer();
	// try {
	// InputSource source = new InputSource(request.getInputStream());
	// source.setEncoding("UTF-8");
	// Document dom = new SAXReader().read(source);
	// Iterator requests =
	// dom.getRootElement().element("requests").elementIterator();
	// buffer.append("<results>");
	// while (requests.hasNext()) {
	// Element req = (Element) requests.next();
	// String serviceName = req.attributeValue("server");
	// String serviceClass = this.serviceMap.get(serviceName);
	// Class c = Class.forName(serviceClass);
	// Element arg = req.element("arg");
	//
	// String returnType = req.attributeValue("returntype");
	// buffer.append("<result id=\"" + req.attributeValue("id") + "\" type=\"" +
	// returnType + "\">");
	// String retVal = execMoted(request, response, c,
	// req.attributeValue("mothed"), arg);
	// if ("x".equals(returnType)) {
	// buffer.append(retVal);
	// } else {
	// buffer.append("<![CDATA[");
	// buffer.append(retVal);
	// buffer.append("]]>");
	// }
	// buffer.append("</result>");
	// }
	// buffer.append("</results>");
	// printToPage(response, buffer);
	//
	// } catch (Exception e) {
	// throw new ServletException(e);
	// }
	//
	// }
	//
	// private void printToPage(HttpServletResponse response, StringBuffer data)
	// throws ServletException, IOException {
	//
	// response.setCharacterEncoding("utf-8");
	// response.setContentType("text/xml");
	// PrintWriter writer = response.getWriter();
	// writer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
	// writer.append("<root errorNum=\"0\">");
	// writer.append("<discription></discription>");
	// writer.append(data);
	// writer.append("</root>");
	// writer.flush();
	// }
	//
	// @SuppressWarnings("unchecked")
	// private String execMoted(HttpServletRequest request, HttpServletResponse
	// response, Class c, String mothed,
	// Element arg) throws Exception {
	// Class classArray[] = new Class[] { HttpServletRequest.class,
	// HttpServletResponse.class, null };
	// Object objArray[] = new Object[] { request, response, null };
	// char type = arg.attributeValue("type").charAt(0);
	// switch (type) {
	// case 'x':// xml
	// classArray[2] = Element.class;
	// objArray[2] = arg.elements().get(0);
	// break;
	// case 'o':// jsonObject
	// classArray[2] = Map.class;
	// objArray[2] = Tools.jsonStr2object(arg.getText(),
	// TypeFactory.mapType(HashMap.class, String.class, Object.class)) ;
	// break;
	// case 'a':// jsonarray
	// classArray[2] = JSONArray.class;
	// objArray[2] = new JSONArray(arg.getText());
	// break;
	// case 's':// string
	// classArray[2] = String.class;
	// objArray[2] = arg.getText();
	// break;
	// default:// null
	// classArray = new Class[] { HttpServletRequest.class,
	// HttpServletResponse.class };
	// objArray = new Object[] { request, response };
	// break;
	// }
	// Method m = c.getMethod(mothed, classArray);
	// return m.invoke(c.newInstance(), objArray).toString();
	//
	// }

}
